package com.jred.jforum.actions.reply;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.jred.jforum.IConst;
import com.jred.jforum.actions.common.TDDispatchAction;
import com.jred.jforum.beans.forum.Message;
import com.jred.jforum.beans.user.User;
import com.jred.jforum.exception.SystemException;
import com.jred.jforum.service.ForumService;

public class ReplyAction extends TDDispatchAction{

	/**
	 * 简单回复
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws SystemException
	 */
    public ActionForward simpleReply(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws SystemException {
            HttpSession session = request.getSession();
            ForumService forumService = (ForumService)getServiceFacade("forumService");
            String fid = (String)request.getParameter("fid");
            String tid = (String)request.getParameter("tid");
            
            User user = (User)session.getAttribute(IConst.SESSION.USER_KEY);
			if (user == null) {
				return (mapping.findForward("jforum-denied"));
			}
            Message msg = new Message();
            msg.setSender(user.getName());
            msg.setContents((String)request.getParameter("contents"));
            msg.setHeading((String)request.getParameter("heading"));
            msg.setIntime(new Date());
            msg.setIp("127.0.0.1");
            msg.setThreadid(Integer.parseInt(tid));
            
            forumService.addMessage(msg);
            
            
            StringBuffer sb = new StringBuffer();
            sb.append("/ShowThread.do?fid=");
            sb.append(fid);
            sb.append("&tid=");
            sb.append(tid);
            return (new ActionForward(sb.toString(), true));
        }
}
